<template>
  <div class="chat-input-container">
    <div class="input-wrapper">
      <input 
        type="text" 
        v-model="message" 
        @keyup.enter="sendMessage" 
        placeholder="请输入消息..." 
        :disabled="isLoading"
        ref="inputField"
      />
      <div class="input-actions">
        <button 
          class="action-btn clear-btn" 
          v-if="message.trim()" 
          @click="clearMessage" 
          title="清空输入"
        >
          ✕
        </button>
      </div>
    </div>
    <button 
      class="send-btn" 
      @click="sendMessage" 
      :disabled="isLoading || !message.trim()"
      :class="{ 'sending': isLoading }"
    >
      <span v-if="isLoading" class="loading-dots">
        <span class="dot"></span>
        <span class="dot"></span>
        <span class="dot"></span>
      </span>
      <span v-else>发送</span>
    </button>
  </div>
</template>

<script>
export default {
  name: 'ChatInput',
  props: {
    isLoading: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      message: ''
    }
  },
  methods: {
    sendMessage() {
      if (this.message.trim() && !this.isLoading) {
        this.$emit('send', this.message)
        this.message = ''
        // 自动聚焦输入框
        this.$nextTick(() => {
          this.$refs.inputField.focus()
        })
      }
    },
    clearMessage() {
      this.message = ''
      this.$refs.inputField.focus()
    }
  },
  mounted() {
    // 组件挂载后自动聚焦输入框
    this.$refs.inputField.focus()
  }
}
</script>

<style scoped>
.chat-input-container {
  display: flex;
  padding: 15px;
  border-top: 1px solid var(--border-color);
  background-color: white;
  position: relative;
  z-index: 10;
  box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.05);
}

.input-wrapper {
  flex: 1;
  position: relative;
  margin-right: 10px;
}

.input-wrapper input {
  width: 100%;
  padding: 12px 40px 12px 15px;
  border: 1px solid #ddd;
  border-radius: var(--border-radius);
  font-size: 16px;
  transition: var(--transition);
  box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.05);
}

.input-wrapper input:focus {
  outline: none;
  border-color: var(--primary-color);
  box-shadow: 0 0 0 3px rgba(74, 144, 226, 0.1);
}

.input-wrapper input:disabled {
  background-color: #f9f9f9;
  cursor: not-allowed;
}

.input-actions {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  display: flex;
  align-items: center;
}

.action-btn {
  background: none;
  border: none;
  color: #aaa;
  cursor: pointer;
  font-size: 14px;
  padding: 5px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: var(--transition);
}

.action-btn:hover {
  color: var(--text-color);
  background-color: #f0f0f0;
}

.clear-btn {
  width: 24px;
  height: 24px;
}

.send-btn {
  padding: 12px 20px;
  min-width: 80px;
  background: linear-gradient(135deg, var(--primary-color), var(--primary-dark));
  color: white;
  border: none;
  border-radius: var(--border-radius);
  cursor: pointer;
  font-weight: 500;
  transition: var(--transition);
  position: relative;
  overflow: hidden;
}

.send-btn:not(:disabled):hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
}

.send-btn:disabled {
  background: #cccccc;
  cursor: not-allowed;
}

.send-btn.sending {
  background: var(--primary-dark);
}

.loading-dots {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
}

.dot {
  width: 6px;
  height: 6px;
  background-color: white;
  border-radius: 50%;
  display: inline-block;
  animation: bounce 1.4s infinite ease-in-out both;
}

.dot:nth-child(1) {
  animation-delay: -0.32s;
}

.dot:nth-child(2) {
  animation-delay: -0.16s;
}

@keyframes bounce {
  0%, 80%, 100% { 
    transform: scale(0);
  } 
  40% { 
    transform: scale(1.0);
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .chat-input-container {
    padding: 10px;
  }
  
  .input-wrapper input {
    padding: 10px 35px 10px 12px;
  }
  
  .send-btn {
    padding: 10px 15px;
    min-width: 70px;
  }
}
</style> 